/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.h2x.parser;

import elaborate.h2x.transform.DefaultAsRootSettingImpl;
import elaborate.h2x.transform.TransformingModel;
import elaborate.h2x.transform.TransformingNode;
import elaborate.h2x.transform.service.Services;
import java.net.MalformedURLException;
import java.net.URL;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

/**
 *
 * @author lendle
 */
public class DefaultAsRootSettingFactoryImpl implements AsRootSettingFactory{

    public void createAsRootSetting(Services services, TransformingModel model, TransformingNode node) throws ParserException {
        Element element=(Element) node.getOriginalNode();
        Attr asRootAttr = element.getAttributeNodeNS("h2x", "as_root");
        String asRootAttrValue=asRootAttr.getValue();
        if(asRootAttrValue==null || asRootAttrValue.length()==0){
            throw new ParserException("empty as_root setting");
        }
        String [] asRoots=asRootAttrValue.split(";");
        if(asRoots==null || asRoots.length==0){
            throw new ParserException("empty as_root setting");
        }
        for(String asRootSetting : asRoots){
            DefaultAsRootSettingImpl asRoot=new DefaultAsRootSettingImpl();
            try{
                String [] array=asRootSetting.split(",");
                if(array==null || array.length==0){
                    throw new ParserException("error as_root setting: "+asRootSetting);
                }
                else if(array.length==1){
                    this.parse(array[0], asRoot, services, model, node, element);
                }
                else if(array.length==2){
                    this.parse(array[0], array[1], asRoot, services, model, node, element);
                }
            }catch(Exception e){
                throw new ParserException(e);
            }
        }
    }

    protected DefaultAsRootSettingImpl parse(String target, String url_exp, DefaultAsRootSettingImpl asRoot, Services services, TransformingModel model, TransformingNode node, Element element) throws MalformedURLException{
        TransformingNode targetNode=services.getLocator().locateNode(model, node, target);
        return this.parse(targetNode, url_exp, asRoot, services, model, node, element);
    }
    
    protected DefaultAsRootSettingImpl parse(String url_exp, DefaultAsRootSettingImpl asRoot, Services services, TransformingModel model, TransformingNode node, Element element) throws MalformedURLException{
        return this.parse(node, url_exp, asRoot, services, model, node, element);
    }
    
    protected DefaultAsRootSettingImpl parse(TransformingNode target, String url_exp, DefaultAsRootSettingImpl asRoot, Services services, TransformingModel model, TransformingNode node, Element element) throws MalformedURLException{
        asRoot.setSchema(new URL(url_exp));
        asRoot.setTarget(target.getId());
        target.setAsRootSetting(asRoot);
        return asRoot;
    }
}
